<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
// 修改setup部分
const route = useRoute()
const taskId = ref(route.params.taskId)
const taskName = ref(route.query.taskName)

// 在模板中使用参数
// <h2>{{ taskName }} - 标注编辑界面</h2>

// 示例数据
const datasetList = ref([
  {
    name: '拉布拉多检测数据',
    description: '大狗检测，准备放100张拉布拉多',
    total: 100,
    labeled: 60,
  },
  // 更多数据集...
])

const stats = ref([
  { label: '标签种类', value: '12类' },
  { label: '已标注图片', value: '60张' },
  { label: '白图比例', value: '5%' },
])

const refreshStats = () => {
  // 实现统计刷新逻辑
}

import { useRouter } from 'vue-router'

const router = useRouter()

const handleLeave = () => {
  // 跳转回数据标注主界面
  router.push('/dataProcess/dataAnnotation')
}

// 模拟获取任务详情的API请求
const fetchTaskDetail = async (taskId: string) => {
  // 模拟api.md#L670-715的请求
  return {
    operation: 'goto_label',
    params: {
      name: '示例数据包',
      brief: '图像分类标注数据集',
      total: 1500,
      labeled_count: 800,
      label_stats: {
        capybaras: 350,
        dogs: 450,
      },
    },
    status: 1,
  }
}

// 模拟开始标注的API请求
const startAnnotation = async (taskId: string) => {
  // 模拟api.md#L717-752的请求
  return {
    operation: 'start_label',
    params: '',
    status: 1,
  }
}

// 模拟封包的API请求
const completeAnnotation = async (taskId: string) => {
  // 模拟api.md#L799-844的请求
  return {
    operation: 'label_over',
    params: [{ name: '大狗检测', desc: '检测大狗品种及位置', labelstatus: 'labeled' }],
    status: 1,
  }
}

// 在setup中添加onMounted钩子
onMounted(async () => {
  const route = useRoute()
  const taskId = route.params.taskId

  const response = await fetchTaskDetail(taskId as string)
  if (response.status === 1) {
    datasetList.value = [
      {
        name: response.params.name,
        description: response.params.brief,
        total: response.params.total,
        labeled: response.params.labeled_count,
      },
    ]

    stats.value = [
      { label: '标签种类', value: `${Object.keys(response.params.label_stats).length}类` },
      { label: '已标注图片', value: `${response.params.labeled_count}张` },
      { label: '白图比例', value: '5%' },
    ]
  }
})

// 修改handleComplete方法
const handleComplete = async () => {
  ElMessageBox.confirm('确定要完成标注并封包吗？该操作不可逆转', '确认封包操作', {
    confirmButtonText: '确认封包',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      const route = useRoute()
      const taskId = route.params.taskId

      const response = await completeAnnotation(taskId as string)
      if (response.status === 1) {
        ElMessage.success('封包操作已执行')
        router.push('/dataProcess/dataAnnotation')
      }
    })
    .catch(() => {})
}

// 添加开始标注的方法
const startLabeling = async () => {
  const route = useRoute()
  const taskId = route.params.taskId

  const response = await startAnnotation(taskId as string)
  if (response.status === 1) {
    ElMessage.success('标注工具已启动')
  }
}
</script>
<template>
  <div class="annotation-editor">
    <!-- 上半部双栏布局 -->
    <el-row class="top-section" :gutter="20">
      <!-- 左栏数据表格 -->
      <el-col :span="14">
        <div class="data-table-container">
          <el-table :data="datasetList" border style="width: 100%">
            <el-table-column type="index" label="序号" width="80" />
            <!-- 纵向表头列 -->
            <el-table-column label="属性项" width="120">
              <template #default="{ row }">
                <div class="vertical-header">
                  <div class="header-item">数据包名称</div>
                  <div class="header-item">简述</div>
                  <div class="header-item">总量</div>
                  <div class="header-item">已标注</div>
                </div>
              </template>
            </el-table-column>

            <!-- 纵向数据列 -->
            <el-table-column label="属性值">
              <template #default="{ row }">
                <div class="vertical-data">
                  <div class="data-item">{{ row.name }}</div>
                  <div class="data-item">{{ row.description }}</div>
                  <div class="data-item">{{ row.total }}</div>
                  <div class="data-item">
                    <span>{{ row.labeled }}</span>
                    <el-progress :percentage="(row.labeled / row.total) * 100" :show-text="false" />
                  </div>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>

      <!-- 右栏统计面板 -->
      <el-col :span="10">
        <div class="stats-panel">
          <div class="stats-content">
            <h4>数据集统计信息</h4>
            <div class="stats-list">
              <div v-for="(stat, index) in stats" :key="index" class="stat-item">
                <span class="label">{{ stat.label }}：</span>
                <span class="value">{{ stat.value }}</span>
              </div>
            </div>
            <el-button class="refresh-btn" type="primary" circle @click="refreshStats">
              <el-icon><Refresh /></el-icon>
            </el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <!-- 底部按钮区 -->
    <div class="action-buttons">
      <el-tooltip content="暂时离开标注工程" placement="top">
        <el-button type="primary" @click="handleLeave">暂离</el-button>
      </el-tooltip>

      <el-popover placement="top" trigger="click">
        <template #reference>
          <el-button type="warning" class="main-action">
            开启标注
            <el-icon class="arrow"><ArrowDown /></el-icon>
          </el-button>
        </template>
        <div class="annotation-tips">
          使用X-anylabeling和中文外挂工具，<br />
          未标注图片将自动生成空标注文件
        </div>
      </el-popover>

      <el-button type="danger" @click="handleComplete">完成并封包</el-button>
    </div>
  </div>
</template>

<style scoped>
.vertical-header,
.vertical-data {
  display: flex;
  flex-direction: column;
  gap: 15px;
  padding: 10px 0;
}

.header-item {
  font-weight: 600;
  color: #1e3a8a;
  height: 40px;
  display: flex;
  align-items: center;
}

.data-item {
  min-height: 40px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.annotation-editor {
  height: 100vh;
  padding: 20px;
  /* 新增宽度设置 */
  width: 100%;
  max-width: 1600px; /* 可根据需要调整具体数值 */
  margin: 0 auto;
}

.top-section {
  height: calc(100% - 80px);
  border-bottom: 1px solid #e0e0e0; /* 浅灰色分栏线 */
}

.data-table-container {
  height: 100%;
  padding: 15px;
  background: white;
  border-right: 2px solid #e0e0e0;
}

:deep(.el-table th) {
  background-color: #1e3a8a; /* 深蓝色背景 */
  color: white;
}

.progress-cell {
  display: flex;
  align-items: center;
  gap: 10px;
}

.stats-panel {
  height: 100%;
  padding: 15px;
}

.stats-content {
  background: #fff3cd; /* 亮黄色背景 */
  padding: 20px;
  border-radius: 8px;
  position: relative;
  min-height: 200px;
}

.stats-list {
  margin-top: 15px;
}

.stat-item {
  margin: 10px 0;
  display: flex;
  justify-content: space-between;
}

.refresh-btn {
  position: absolute;
  bottom: 15px;
  right: 15px;
}

.action-buttons {
  height: 80px;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 40px;
  padding-top: 20px;
}

.main-action {
  position: relative;
  padding-right: 40px;
}

.arrow {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
}

.annotation-tips {
  padding: 10px;
  font-size: 0.9em;
  color: #666;
}
</style>
